<template>
  <el-form ref="loginFormRef" class="login_job" :model="loginForm" :rules="loginRules" size="default">
    <el-form-item prop="staffNum">
      <el-input v-model="loginForm.staffNum" class="login_input" placeholder="工号" size="default"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input
        v-model="loginForm.password"
        class="login_input"
        type="password"
        placeholder="密码"
        show-password
        autocomplete="new-password"
        size="default"
      ></el-input>
    </el-form-item>
  </el-form>
  <!-- <el-button :icon="CircleClose" round @click="resetForm(loginFormRef)" size="default">重置</el-button> -->
  <el-button class="login-btn" size="default" type="primary" :loading="loading" @click="loginSubmit(loginFormRef)">
    登录
  </el-button>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElForm } from 'element-plus'
import { useUserStore } from '@/store/user'
const store = useUserStore()
// 定义 formRef（校验规则）
// type FormInstance = InstanceType<typeof ElForm>
const loginFormRef = ref()
const loginForm = reactive({
  staffNum: '',
  password: ''
})
const loginRules = reactive({
  staffNum: [{ required: true, message: '请输入工号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
})

const loading = ref(false)
const router = useRouter()
// login
const loginSubmit = (formEl) => {
  // debugger
  // console.log(123, formEl)
  if (!formEl) return

  formEl.validate(async (valid) => {
    // console.log(456, valid)
    if (!valid) return false
    const userInfo = {
      staffNum: loginForm.staffNum,
      password: loginForm.password
    }
    store
      .loginByStaffNum(userInfo)
      .then(() => {
        // debugger

        // store.loginSessionRefresh();
        loading.value = false
        const redicts = window.location.href.split('?redirect=')
        const redirect = redicts && redicts.length ? redicts[1] : ''

        router.push({
          path: redirect || '/'
        })
      })
      .catch((err) => {
        console.log(789, err)

        // debugger
        loading.value = false
      })
  })
}

// resetForm 重置
// const resetForm = (formEl: FormInstance | undefined) => {
// 	if (!formEl) return;
// 	formEl.resetFields();
// };

onMounted(() => {
  // 监听enter事件（调用登录）
  document.onkeydown = (e) => {
    e = window.event || e
    if (e.code === 'Enter' || e.code === 'enter' || e.code === 'NumpadEnter') {
      if (loading.value) return
    }
  }
})
</script>

<style scoped lang="scss">
$cursor: gray;
.login_job {
  .login_input {
    height: 46px;
    input {
      -webkit-appearance: none;
      padding: 12px 5px 12px 15px;
      color: #000;
      height: 47px;
      border: 1px solid #ecf0f6;
      background: #f8f8f8;
      caret-color: $cursor;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: #000 !important;
      }
    }
  }
}
.login-btn {
  margin-top: 30px;
  width: 100%;
}
</style>
